﻿using UnityEngine;
using System.Collections;
using Assets.Game.scripts.ui.models;
using Engine.Entities.Character;

namespace Assets.Game.scripts.ui.controllers {

    [RequireComponent(typeof(PartyWidgetModel))]
    public class PartyWidgetController : MonoBehaviour {

        public UIGrid partyGrid;
        public UIGrid partyPeopleGrid;
        public UIScrollView partyScrollView;
        public UIScrollView peopleScrollView;
        public GameObject partyItemRenderer;
        public GameObject peopleItemRenderer;
        public GameObject addItemRenderer;

        private bool isRequiredUpdate = false;
        private GameObject lastRendererBackFlipped = null;

        void Start() {
            this.isRequiredUpdate = false;
            this.lastRendererBackFlipped = null;
            PartyWidgetModel model = this.GetComponent<PartyWidgetModel>();

            NGUITools.DestroyChildren(this.partyGrid.transform);
            NGUITools.DestroyChildren(this.partyPeopleGrid.transform);

            //aggiunge le comitive
            if (this.partyGrid != null && model != null) {
                //aggiungo il pulsante di add nuova comitiva
                if (this.addItemRenderer != null) { 
                    GameObject addItemRendererInstance = NGUITools.AddChild(this.partyGrid.gameObject, this.addItemRenderer);
                    EventDelegate addCallback = new EventDelegate(this, "onAddNewParty");
                    UIButton addRendererButton = addItemRendererInstance.GetComponent<UIButton>();
                    if (addRendererButton != null) {
                        EventDelegate.Set(addRendererButton.onClick, addCallback);
                    }
                }

                foreach (PartyController party in model.peopleController.PartyList) {
                    //creo il renderer e il suo model
                    GameObject itemRendererInstance = NGUITools.AddChild(this.partyGrid.gameObject, this.partyItemRenderer);
                    PartyItemRendererModel itemRendererModel = itemRendererInstance.GetComponent<PartyItemRendererModel>();
                    if (itemRendererModel != null) {
                        itemRendererModel.party = party;
                    }

                    //associo una callback per il click
                    EventDelegate callback = new EventDelegate(this, "onPartyClick");
                    callback.parameters[0].value = itemRendererInstance;
                    callback.parameters[1].value = itemRendererModel;
                    EventDelegate removeCallback = new EventDelegate(this, "onPartyRemove");
                    removeCallback.parameters[0].value = itemRendererModel; 
                    UIButton rendererButton = itemRendererInstance.GetComponent<UIButton>();
                    PartyItemRendererController controller = itemRendererInstance.GetComponent<PartyItemRendererController>();
                    if (rendererButton != null) {
                        EventDelegate.Set(rendererButton.onClick, callback);
                    }

                    if (controller != null) {
                        EventDelegate.Set(controller.onDeleteParty, removeCallback);
                    }
                }
            }

            this.isRequiredUpdate = true;
        }

        void Update() { 
            if(this.isRequiredUpdate){
                if(this.partyScrollView!=null){
                    this.partyScrollView.ResetPosition();
                    this.partyScrollView.UpdatePosition();
                    this.partyGrid.Reposition();
                }

                if(this.peopleScrollView!=null){
                    this.peopleScrollView.ResetPosition();
                    this.peopleScrollView.UpdatePosition();
                    this.partyPeopleGrid.Reposition();
                }
                this.isRequiredUpdate = false;
            }
        }




        public void onPartyRemove(PartyItemRendererModel partyRemoved) {
            //cancello la comitiva
            FrenzyPeopleController.instance.DestroyParty(partyRemoved.party);

            //svuoto la lista degli individui attuali
            NGUITools.DestroyChildren(this.partyPeopleGrid.transform);
        }




        public void onPartyClick(GameObject itemRenderer, PartyItemRendererModel partyClicked) {
            //svuoto la lista degli individui attuali
            NGUITools.DestroyChildren(this.partyPeopleGrid.transform);

            if (this.lastRendererBackFlipped != itemRenderer) {
                //giro l'ultimo renderer trovato
                if (this.lastRendererBackFlipped != null) {
                    this.lastRendererBackFlipped.SendMessage("OnClick");
                }

                //una volta cliccata una comitiva posso mostrare gli individui al di sotto
                if (this.partyPeopleGrid != null && partyClicked.party != null && this.partyItemRenderer != null) {
                    foreach (SteveBrain agent in partyClicked.party.PeopleInParty) {
                        //creo il renderer e il suo model
                        SteveModel agentModel = agent.model;
                        GameObject itemRendererInstance = NGUITools.AddChild(
                            this.partyPeopleGrid.gameObject,
                            this.peopleItemRenderer
                        );

                        CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                        if (itemModel != null) {
                            itemModel.data = agent;
                        }
                    }

                    this.isRequiredUpdate = true;
                }

                this.lastRendererBackFlipped = itemRenderer;
            } else {
                //se clicco due volte sullo stesso oggetto allora è come se non avessi premuto niente
                this.lastRendererBackFlipped = null;
            }
        }

        public void onAddNewParty() {
            FrenzyPeopleController.instance.CreateParty();
            this.Start();
        }


        
        public void onClose() {
            NGUITools.Destroy(this.gameObject);
        }

    }

}